/*
 *
 */
package sentinel;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassAnnotationMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.MethodInheritanceAnnotationMatcher;

import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.match.MethodInheritanceAnnotationMatcher.byMethodInheritanceAnnotationMatcher;

public class SentinelInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {

    private static final String ENHANCE_ANNOTATION = "org.springframework.web.bind.annotation.RestController";

    private static final String INTERCEPT_CLASS = "sentinel.SentinelInterceptor";
//    private static final String INTERCEPT_CLASS_CON = "org.apache.skywalking.apm.plugin.sentinel.SentinelConInterceptor";
//    public static final String ENHANCE_ANNOTATION_CON = "org.springframework.stereotype.Controller";

    @Override
    protected ClassMatch enhanceClass() {
        return ClassAnnotationMatch.byClassAnnotationMatch(new String[]{ENHANCE_ANNOTATION});
    }

    @Override
    public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
        return null;
    }

    @Override
    public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[]{
                new InstanceMethodsInterceptPoint() {
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return MethodInheritanceAnnotationMatcher.byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.GetMapping"))
                                .or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.PostMapping")))
                                .or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.PutMapping")))
                                .or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.DeleteMapping")))
                                .or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.PatchMapping")));

                    }

                    @Override
                    public String getMethodsInterceptor() {
                        return INTERCEPT_CLASS;
                    }

                    @Override
                    public boolean isOverrideArgs() {
                        return false;
                    }
                }
//                ,
//                new InstanceMethodsInterceptPoint() {
//                    @Override
//                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
//                        return MethodInheritanceAnnotationMatcher.byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.RequestMapping"));
//                    }
//
//                    @Override
//                    public String getMethodsInterceptor() {
//                        return INTERCEPT_CLASS_CON;
//                    }
//
//                    @Override
//                    public boolean isOverrideArgs() {
//                        return false;
//                    }
//                }
        };
    }
}
